package com.pipayshop.controller;

import com.pipayshop.entity.dto.HotelLoginDTO;
import com.pipayshop.entity.dto.HotelShopInfoListByConditionDTO;
import com.pipayshop.entity.dto.HotelUserAdminDTO;
import com.pipayshop.entity.vo.*;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.HotelShopInfoService;
import com.pipayshop.service.HotelShopOrderInfoService;
import com.pipayshop.service.HotelUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 实体店酒店的信息 前端控制器
 * </p>
 *
 * @author nws
 * @since 2023-07-25
 */
@Api(value = "实体店酒店的信息接口",tags = "实体店酒店的信息接口")
@RestController
@RequestMapping("/pipayshopapi/shop-info")
@Slf4j
public class HotelInfoController {
    @Resource
    private HotelShopInfoService infoService;
    @Resource
    private HotelUserInfoService userInfoService;
    @Resource
    private HotelShopOrderInfoService orderInfoService;



    @GetMapping("getShopInfoByUid/{uid}")
    @ApiOperation("根据uid查询酒店详情")
    public ResponseVO getShopInfoByUid(@PathVariable String uid) {
        try {
            HotelShopInfoVO shopByIdVO = infoService.getShopInfoByUid(uid);
            return ResponseVO.getSuccessResponseVo(shopByIdVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("query_hotel_details_by_uid_failed_contact_admin");
        }
    }

    @GetMapping("getShopInfoByShopId/{shopId}")
    @ApiOperation("根据酒店id查询酒店房间信息")
    public ResponseVO getShopInfoByShopId(@PathVariable String shopId) {
        try {
            List<RoomListVO> shopInfoVO = infoService.getShopInfoByShopId(shopId);
            return ResponseVO.getSuccessResponseVo(shopInfoVO);
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("query_hotel_rooms_by_id_failed_contact_admin");
        }
    }

    // 查询所有酒店订单
    @GetMapping("getAllOrderByShopId/{shopId}")
    @ApiOperation("根据uid查询所有酒店订单")
    public ResponseVO getAllOrderByShopId(@PathVariable String shopId) {
        try {
            List<HotelStoreOrderInfoVO> shopInfoVO = orderInfoService.getAllOrderByShopId(shopId);
            return ResponseVO.getSuccessResponseVo(shopInfoVO);
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("retrieve_hotel_room_info_failed_contact_staff");
        }
    }

    // 查询每个区域的酒店数量
    @PostMapping("/countByRegionIds")
    @ApiOperation("查询每个区域的酒店数量")
    public ResponseVO<Map<String, Long>> getHotelCountByRegion(@RequestBody List<Map<String, String>> regions) {
        try {
            List<String> regionIds = regions.stream()
                    .map(region -> region.get("countryId"))
                    .collect(Collectors.toList());
            Map<String, Long> result = infoService.getHotelCountsByRegionIds(regionIds);
            return ResponseVO.getSuccessResponseVo(result);
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("query_hotel_count_by_region_failed_contact_staff");
        }

    }

    @GetMapping("getHotelInfoByCondition")
    @ApiOperation("根据条件筛选酒店信息")
    public ResponseVO<PageDataVO> getHotelInfoByCondition(LivePageVO livePageVO) {
        try {
            PageDataVO pageDataVO = infoService.getHotelInfoByCondition(livePageVO);
            if (pageDataVO == null) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo(pageDataVO);
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("filter_hotel_info_by_conditions_failed_contact_staff");
        }
    }

    @PostMapping("getShopInfoListByCondition")
    @ApiOperation("首页根据条件获取所有实体店酒店列表")
    public ResponseVO<PageDataVO> getShopInfoListByCondition(@RequestBody HotelShopInfoListByConditionDTO hotelShopInfoListByConditionDTO){
        try {
            PageDataVO shopInfoListByCondition = infoService.getShopInfoListByCondition(hotelShopInfoListByConditionDTO);
            return ResponseVO.getSuccessResponseVo(shopInfoListByCondition);
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_all_hotel_list_failed_contact_staff");
        }
    }

    @GetMapping("getShopInfoById/{shopId}")
    @ApiOperation("根据id查询实体店详情")
    public ResponseVO getShopInfoById(@PathVariable String shopId) {
        try {
            ShopInfoVO shopByIdVO = infoService.getShopInfoById(shopId);
            return ResponseVO.getSuccessResponseVo(shopByIdVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("query_store_details_by_id_failed_contact_staff");
        }
    }

}
